<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/27
 * Time: 10:17
 */

namespace app\index\logic;

use app\index\model\Alibill;
use app\index\model\Order;
use app\index\service\AlipayTradeService;
use app\index\service\builder\AlipayTradePayContentBuilder;

class AliScanPay extends BaseLogic
{
    public function aliPay($param)
    {
        $param['title'] = empty($param['title']) ? '测试' : $param['title'];
        $barPayRequestBuilder = new AlipayTradePayContentBuilder();
        $barPayRequestBuilder->setOutTradeNo($param['sn']);
        $barPayRequestBuilder->setTotalAmount($param['totalAmount']);
        $barPayRequestBuilder->setAuthCode($param['authCode']);
        $barPayRequestBuilder->setSubject($param['title']);
        //校对金额
        $order = Order::where('title', $param['sn'])->find();

        if (empty($order)) {
            return $this->returnMsg('1002', '订单不存在', '');
        }
        if ($param['totalAmount'] != $order['money'] || $param['totalAmount'] <= 0) {
            return $this->returnMsg('1002', '金额不正确', '');
        }
        //当面付
        $barPay = new AlipayTradeService(config('ali_config'));
        $barPayResult = $barPay->barPay($barPayRequestBuilder);
        //日志文件
        $log_file = 'ali_notify' . date('Y-m-d');
        file_put_contents('./log/' . $log_file . '.txt', date("Y-m-d H:i:s") . " 返回结果 " . json_encode($barPayResult->getResponse()) . "\r\n", FILE_APPEND);
        //获得支付结果
        $data = [];
        switch ($barPayResult->getTradeStatus()) {
            case "SUCCESS":
                $code = '1001';
                $msg = '支付成功';
                $data = ['pay_amount' => $barPayResult->getResponse()->total_amount,
                    'buyer_phone' => $barPayResult->getResponse()->buyer_logon_id,
                    'sn' => $barPayResult->getResponse()->out_trade_no,];
                //修改状态
                $result = set_curl('http://syapi.zknfmy.com/Index/jxcorder/finish', ['sn' => $param['sn']]);
                break;
            case "FAILED":
                $code = '1002';
                $msg = $barPayResult->getResponse()->sub_msg;
                break;
            case "UNKNOWN":
                $code = '1002';
                $msg = '异常';
                if ($barPayResult->getResponse()->sub_msg) {
                    $msg = $barPayResult->getResponse()->sub_msg;
                }
                break;
            default:
                $code = '1002';
                $msg = '不支持的交易状态';
                break;
        }

        $this->addPayRecord($barPayResult->getResponse(), $barPayResult->getTradeStatus());

        return $this->returnMsg($code, $msg, $data);
    }

    /**
     * 支付记录
     * @param $payResult
     * @param $status
     */
    public function addPayRecord($payResult, $status)
    {
        $add_arr = [];
        $add_arr ['timeline'] = time();
        $add_arr ['user_phone'] = $payResult->buyer_logon_id;
        $add_arr ['money'] = $payResult->buyer_pay_amount;
        $add_arr ['trade_no'] = $payResult->trade_no;
        $add_arr ['sn'] = $payResult->out_trade_no;
        switch ($status) {
            case "SUCCESS":
                $add_arr['status'] = 2;
                break;
            case "FAILED":
                $add_arr['status'] = 3;
                $add_arr['intro'] = $payResult->sub_msg;;
                break;
            case "UNKNOWN":
                $add_arr['status'] = 3;
                break;
            default:
                break;
        }
        $res = Alibill::create($add_arr);

    }

}